package com.ruoyi.ruoyimaintenance.mapper;

import com.ruoyi.ruoyimaintenance.domain.entity.MaintenanceDeviceConfig;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MaintenanceDeviceConfigMapper {

    @Results(id = "maintenanceRecordMap", value = {
            @Result(property = "id", column = "id", id = true), // 通常id字段为主键
            @Result(property = "machineNumber", column = "machine_number"),
            @Result(property = "machineHead", column = "machine_head"),
            @Result(property = "headNumber", column = "head_number"),
            @Result(property = "maintenanceFrequency", column = "maintenance_frequency"),
            @Result(property = "warningDays", column = "warning_days")
    })
    @Select("<script>" +
            "select * from maintenance_device_config where  1=1 " +
            "<if test='line != null and line !=\"\" '> and line=#{line}</if>"+
            "<if test='machineNumber != null and machineNumber !=\"\" '> and " +
            " machine_number like CONCAT('%', #{machineNumber}, '%')</if>"+
            "<if test='headNumber != null and headNumber !=\"\" '> and " +
            " head_number like CONCAT('%', #{headNumber}, '%')</if>"+
            "</script>")
    List<MaintenanceDeviceConfig> maintenanceDeviceConfigBySearch(@Param("line") Integer line,@Param("machineNumber") String machineNumber,@Param("headNumber") String headNumber);

    @Insert("INSERT INTO maintenance_device_config(line,side,machine_number,machine_head,head_number,maintenance_frequency,warning_days) VALUES" +
            "(#{maintenanceDeviceConfig.line}," +
            "#{maintenanceDeviceConfig.side}," +
            "#{maintenanceDeviceConfig.machineNumber}," +
            "#{maintenanceDeviceConfig.machineHead}," +
            "#{maintenanceDeviceConfig.headNumber}," +
            "#{maintenanceDeviceConfig.maintenanceFrequency}," +
            "#{maintenanceDeviceConfig.warningDays})")
    int addMaintenanceDeviceConfig(@Param("maintenanceDeviceConfig") MaintenanceDeviceConfig maintenanceDeviceConfig);

    @Update("UPDATE maintenance_device_config SET " +
            "line = #{maintenanceDeviceConfig.line}, " +
            "side = #{maintenanceDeviceConfig.side}, " +
            "machine_number = #{maintenanceDeviceConfig.machineNumber}, " +
            "machine_head = #{maintenanceDeviceConfig.machineHead}, " +
            "head_number = #{maintenanceDeviceConfig.headNumber}, " +
            "maintenance_frequency = #{maintenanceDeviceConfig.maintenanceFrequency}, " +
            "warning_days = #{maintenanceDeviceConfig.warningDays} " +
            "WHERE id = #{maintenanceDeviceConfig.id}")
    int updateMaintenanceDeviceConfig(@Param("maintenanceDeviceConfig")MaintenanceDeviceConfig maintenanceDeviceConfig);


    @Delete("delete from maintenance_device_config where id=#{id}")
    int deleteMaintenanceDeviceConfig(@Param("id") Long id);

    @ResultMap("maintenanceRecordMap")
    @Select("select * from maintenance_device_config where id=#{id}")
    MaintenanceDeviceConfig selectMaintenanceDeviceConfigById(@Param("id")long id);
}
